﻿using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcExtensions.Extensions
{
    public static class DialogExtensions
    {
        public static IHtmlString AttachmentLink<TModel>(this HtmlHelper<TModel> htmlHelper, UrlHelper url,
                                                         object htmlAttributes = null,
                                                         string dialogClass = null,
                                                         string dialogTitle = null)
        {
            var attachmentModel = htmlHelper.ViewData.Model as IAttachmentLink;
            if (attachmentModel == null || attachmentModel.EntityId == 0)
            {
                return MvcHtmlString.Empty;
            }

            var entityType = (int)attachmentModel.EntityType;
            var entityId = attachmentModel.EntityId;
            return DialogLinkWithIframe(htmlHelper, url, "",
                                        "Attachments", "Attachment", new { entityType, entityId },
                                        htmlAttributes,
                                        new DialogOptions
                                        {
                                            DialogClass = dialogClass,
                                            DialogTitle = dialogTitle
                                        });
        }

        //public static IHtmlString AttachmentLinks<TModel>(this HtmlHelper<TModel> htmlHelper, UrlHelper url, string dialogClass = null)
        //{
        //    var attachmentModel = htmlHelper.ViewData.Model as IAttachment;
        //    if (attachmentModel == null || attachmentModel.AttachmentEntities == null)
        //    {
        //        return MvcHtmlString.Empty;
        //    }
        //    var sb = new StringBuilder(255);

        //    sb.Append("<div>Attachments</div>");

        //    foreach (var entityAttachment in attachmentModel.AttachmentEntities)
        //    {
        //        var entityType = (int)entityAttachment.EntityType;
        //        var entityId = entityAttachment.EntityId;

        //        var link = DialogLinkWithIframe(htmlHelper, url,
        //                                        entityAttachment.DisplayText ?? entityAttachment.EntityType.ToString(),
        //                                        "Attachments", "Attachment", new { entityType, entityId },
        //                                        dialogptions: new DialogOptions
        //                                        {
        //                                            DialogClass = dialogClass,
        //                                        });
        //        sb.AppendFormat("<div>{0}</div>", link);
        //    }
        //    return MvcHtmlString.Create(sb.ToString());
        //}

        public static IHtmlString DialogLinkWithIframe<TModel>(this HtmlHelper<TModel> htmlHelper, UrlHelper url,
                                                               string linkText,
                                                               string actionName, string controllerName = null,
                                                               object routeValue = null,
                                                               object htmlAttributes = null,
                                                               DialogOptions dialogptions = null)
        {

            var routValueAttributes = GetDialogAttributes(url, actionName, controllerName,
                                                          routeValue, htmlAttributes, dialogptions);
            routValueAttributes.Add("onclick", "showDialogWithIframe(this);");

            var attributes = ControlExtensions.ConvertDictionaryToAttributes(routValueAttributes);

            return MvcHtmlString.Create(string.Format("<a {0}>{1}</a>", attributes, linkText));
        }

        public static IHtmlString DialogButtonWithIframe<TModel>(this HtmlHelper<TModel> htmlHelper, UrlHelper url,
                                                               string text,
                                                               string actionName, string controllerName = null,
                                                               object routeValue = null,
                                                               object htmlAttributes = null,
                                                               DialogOptions dialogptions = null)
        {

            var routValueAttributes = GetDialogAttributes(url, actionName, controllerName,
                                                          routeValue, htmlAttributes, dialogptions);
            routValueAttributes.Add("onclick", "showDialogWithIframe(this);");

            var attributes = ControlExtensions.ConvertDictionaryToAttributes(routValueAttributes);

            return MvcHtmlString.Create(string.Format("<input type=\"button\" value=\"{0}\" {1}/>", text, attributes));
        }

        public static IHtmlString DialogButtonWithUrl<TModel>(this HtmlHelper<TModel> htmlHelper, UrlHelper url,
                                                               string text,
                                                               string actionName, string controllerName = null,
                                                               object routeValue = null,
                                                               object htmlAttributes = null,
                                                               DialogOptions dialogptions = null)
        {

            var routValueAttributes = GetDialogAttributes(url, actionName, controllerName,
                                                          routeValue, htmlAttributes, dialogptions);
            routValueAttributes.Add("onclick", "showDialogWithUrl(this);");

            var attributes = ControlExtensions.ConvertDictionaryToAttributes(routValueAttributes);

            return MvcHtmlString.Create(string.Format("<input type=\"button\" value=\"{0}\" {1}/>", text, attributes));
        }

        private static RouteValueDictionary GetDialogAttributes(UrlHelper url, string actionName, string controllerName,
                                                        object routeValue, object htmlAttributes, DialogOptions dialogptions)
        {
            var routValueAttributes = ControlExtensions.GetAttributeList(htmlAttributes);

            routValueAttributes.Add("data-url", url.Action(actionName, controllerName, routeValue));

            if (dialogptions != null && !string.IsNullOrEmpty(dialogptions.DialogClass))
                routValueAttributes.Add("data-dialogClass", dialogptions.DialogClass);

            if (dialogptions != null && !string.IsNullOrEmpty(dialogptions.DialogTitle))
                routValueAttributes.Add("data-dialogTitle", dialogptions.DialogTitle);

            if (dialogptions != null && !string.IsNullOrEmpty(dialogptions.OnDialogCancel))
                routValueAttributes.Add("data-onDialogCancel", dialogptions.OnDialogCancel);

            if (dialogptions != null && !string.IsNullOrEmpty(dialogptions.OnDialogClose))
                routValueAttributes.Add("data-onDialogClose", dialogptions.OnDialogClose);

            return routValueAttributes;
        }
    }

    public class DialogOptions
    {
        public string DialogTitle { get; set; }
        public string DialogClass { get; set; }

        public string OnDialogClose { get; set; }
        public string OnDialogCancel { get; set; }
    }

    public interface IAttachmentLink
    {
        EntityType EntityType { get; set; }

        int EntityId { get; set; }
    }

    public enum EntityType
    {
        
    }
}
